# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2Json_v2 import Table2Json


class InvitedRecordsDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def insertRecords(self, info):
        """
        添加 invited_records 信息
        :param info:
        :return:
        """
        userId = info['userId']
        invitedUserId = info['invitedUserId']
        code = info['code']
        isReward = info['isReward']

        sql = "insert into zzh_invited_records (user_id, invited_user_id,code,is_reward) VALUES (%s, %s, %s, %s)"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [userId, invitedUserId, code, isReward])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def findRecordsById(self, reId):
        """
        根据ID查询 invited_records 信息
        :param reId:
        :return:
        """
        sql = "select * from zzh_invited_records WHERE id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [reId])
        jsons = None
        if rs:
            list0 = cur.fetchall()
            jsons = Table2Json.invited_records_list_to_json(list0)
        cur.close()
        self.conn.close()
        return jsons

    def findRecordsByCondition(self, **kwargs):
        """
        zzh_invited_records 表的条件查询
        :param kwargs:
        :return:
        """
        userId = kwargs['userId']
        sql = "select * from zzh_invited_records WHERE user_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [userId])
        jsons = None
        if rs:
            list0 = cur.fetchall()
            jsons = Table2Json.invited_records_list_to_json(list0)
        cur.close()
        self.conn.close()
        return jsons

    def deleteRecords(self, idArray):
        """
        根据ID删除信息，可以传递一个或者多个ID，ID以数组形式传递
        :param idArray:
        :return:
        """
        sql = "delete from zzh_invited_records WHERE id=%s"
        cur = self.conn.cursor()
        for idd in idArray:
            rs = cur.execute(sql, [idd])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def updateRecords(self, info):
        """
        更新 invited_records 表
        :param info:
        :return:
        """
        mId = info['id']
        userId = info['userId']
        invitedUserId = info['invitedUserId']
        code = info['code']
        isReward = info['isReward']

        sql = "update zzh_invited_records set user_id=%s,invited_user_id=%s,code=%s,is_reward=%s WHERE id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [userId,invitedUserId,code,isReward, mId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def selectInvite(self, id):
        """
        根据ID查询，返回元组
        :param id:
        :return: data tuple
        """
        sql = "select * from zzh_invited_records WHERE id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [id])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr